package com.xzj.admin.analysis.base.custom.ct;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.custom.common.CommonAnalysis;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

/**
 * @program: xzj-admin
 * @ClassName: GzAnalysisHandle
 * @description: CT云贵州解析方法
 * @author: XiaZunJun
 * @create: 2024-11-12 22:38
 */
public class NmAnalysisHandle extends CommonAbstractAnalysisHandle {

    static {
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.region.name(), CommonAnalysis::regionSub);
        registerJudgeSubFun("osSecurityGroups", NmAnalysisHandle::osSecurityGroupsSub);
        registerJudgeSubFun("trunks", NmAnalysisHandle::trunksSub);
        registerJudgeSubFun("vm", NmAnalysisHandle::vmSub);
    }

    public NmAnalysisHandle(String cloudId,String cloudType) {
        super(cloudId, cloudType,new NmThemeSpecialAnalysisHandle(cloudId,cloudType));
    }

    private static void vmSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        JSONArray volumesAttached = row.getJSONArray(fieldName);
        if(null != volumesAttached && !volumesAttached.isEmpty()){
            for(int i=0;i<volumesAttached.size();i++){
                String id = volumesAttached.getJSONObject(i).getString("id");
                if(StrUtil.isNotEmpty(id)){
                    JSONObject storageDevice = new JSONObject();
                    storageDevice.put("virtualDiskId", id);
                    storageDevice.put("id", AnalysisCommonUtil.commonBuildStr(parentId, id));
                    storageDevice.put("vdeviceId", parentId);
                    subThemeRows.add(storageDevice);
                }
            }

        }
    }

    private static void osSecurityGroupsSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        subThemeRows.addAll(row.getJSONArray(fieldName));
    }

    private static void trunksSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        subThemeRows.addAll(row.getJSONArray(fieldName));
    }
}
